package com.zhijian.wharf.service.impl;

import com.zhijian.BusinessException;
import com.zhijian.response.enums.ResponseCodeEnum;
import com.zhijian.util.page.Page;
import com.zhijian.wharf.dao.UserGroupMapper;
import com.zhijian.wharf.entity.po.UserGroup;
import com.zhijian.wharf.entity.vo.request.UserGroupSearchVo;
import com.zhijian.wharf.entity.vo.response.UserGroupVo;
import com.zhijian.wharf.service.UserGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 人员-组织架构关系表业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-01-24 15:27:14
 */
@Slf4j
@Service
public class UserGroupServiceImpl implements UserGroupService {

    @Autowired
    private UserGroupMapper mapper;

    @Override
    public UserGroup selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public UserGroupVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByUserId(Long userId) {
        mapper.deleteByUserId(userId);
    }

    @Override
    public Long insert(UserGroup userGroup) {
        Date now = new Date();
        userGroup.setCreateTime(now);
        userGroup.setUpdateTime(now);
        return mapper.insert(userGroup) > 0 ? userGroup.getId() : 0L;
    }

    @Override
    public boolean update(UserGroup userGroup) {
        Date now = new Date();
        userGroup.setUpdateTime(now);
        return mapper.updateByPrimaryKey(userGroup) != 0;
    }

    @Override
    public Integer count(UserGroupSearchVo userGroupSearchVo) {
        return mapper.count(userGroupSearchVo);
    }

    @Override
    public List<UserGroupVo> selectList(UserGroupSearchVo userGroupSearchVo) {
        return mapper.selectList(userGroupSearchVo);
    }

    @Override
    public Page<UserGroupVo> selectPage(UserGroupSearchVo userGroupSearchVo) {
        //先直接查总数
        int count = count(userGroupSearchVo);
        //分页
        Page<UserGroupVo> page = new Page<>(true, count, userGroupSearchVo.getPageNum(), userGroupSearchVo.getPageSize());
        userGroupSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<UserGroupVo> responseVos = this.selectList(userGroupSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean connectGroup(Long userId, Long groupId) {
        Integer count = this.count(UserGroupSearchVo.builder()
                .userId(userId)
                .groupId(groupId)
                .build());
        if (count > 0) {
            throw new BusinessException(ResponseCodeEnum.FAILURE.code, "此人员已在此部门，请勿重复操作");
        }
        this.insert(UserGroup.builder()
                .userId(userId)
                .groupId(groupId)
                .build());
        return true;
    }

    @Override
    public List<Long> selectGroupIdsByUserId(Long userId) {
        return mapper.selectGroupIdsByUserId(userId);
    }

}